package com.wtpaint.lib.skin;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by leoxu on 2018/11/18.
 */

public class ZeusSkinView {

    public View view;
    
    public List<ZeusSkinPair> skinPairs;

    public ZeusSkinView(View view, List<ZeusSkinPair> skinPairs) {
        this.view = view;
        this.skinPairs = skinPairs;
    }

    public void applySkin() {
        for (ZeusSkinPair skinPair : skinPairs) {
            Drawable left = null, top = null, right = null, bottom = null;
            switch (skinPair.attributeName) {
                case "background":
                    Object background = ZeusSkinResources.getInstance().getBackground(skinPair
                            .resId);
                    //Color
                    if (background instanceof Integer) {
                        view.setBackgroundColor((Integer) background);
                    } else {
                        ViewCompat.setBackground(view, (Drawable) background);
                    }
                    break;
                case "src":
                    background = ZeusSkinResources.getInstance().getBackground(skinPair
                            .resId);
                    if (background instanceof Integer) {
                        ((ImageView) view).setImageDrawable(new ColorDrawable((Integer)
                                background));
                    } else {
                        ((ImageView) view).setImageDrawable((Drawable) background);
                    }
                    break;
                case "textColor":
                    ((TextView) view).setTextColor(ZeusSkinResources.getInstance().getColorStateList
                            (skinPair.resId));
                    break;
                case "drawableLeft":
                    left = ZeusSkinResources.getInstance().getDrawable(skinPair.resId);
                    break;
                case "drawableTop":
                    top = ZeusSkinResources.getInstance().getDrawable(skinPair.resId);
                    break;
                case "drawableRight":
                    right = ZeusSkinResources.getInstance().getDrawable(skinPair.resId);
                    break;
                case "drawableBottom":
                    bottom = ZeusSkinResources.getInstance().getDrawable(skinPair.resId);
                    break;
                default:
                    break;
            }
            if (null != left || null != right || null != top || null != bottom) {
                ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right,
                        bottom);
            }
        }
    }


}
